package com.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDaox {
	static {//导入MySql驱动包
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {}
	}
	private Connection getConn() throws SQLException {
		String url = "jdbc:mysql://localhost:3306/hi_db" + "?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "15723";
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}
	public Integer addUser(Integer uid,String image) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//3.编写SQL代码
		String sql = "INSERT INTO user(uid,image) "
				+ "VALUES("+uid+",'"+image+"')";
		Integer result = st.executeUpdate(sql);
		//5、关闭执行块
		st.close();
		//6、关闭连接
		conn.close();
		//7、返回结果
		return result;
	}
	public boolean findUserByBC(
			Integer uid) throws SQLException {
		//1、创建连接
		Connection conn = getConn();
		//2、创建处理块
		Statement st = conn.createStatement();
		//3、编写sql
		String str = "SELECT * FROM user WHERE"
				+ " uid="+uid+"";
		//4、执行sql
		ResultSet rs = st.executeQuery(str);
		//5、判断rs是否有内存
		if(rs.next()) {//有此分类
			rs.close();
			st.close();
			conn.close();
			return true;
		}
		rs.close();
		st.close();
		conn.close();
		return false;
	}

	public String findpassword(Integer uid) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//3.编写SQL代码
		String sql = "SELECT * FROM user WHERE "
			+ "uid="+uid+"";
		//4、执行SQL
		ResultSet rs = st.executeQuery(sql);
		if(rs.next()) {//为true代表有数据
			String pasd =rs.getString("image");
					
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return pasd;//返回查询到的用户信息
		}
		else {
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return null;
		}
	}
	public Integer findpasd(Integer uid,String image) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//3.编写SQL代码
		String  str = "UPDATE  user set Uhp ='"+image+"'WHERE "
			+ "Uid="+uid+"";
		//4、执行SQL
		Integer result = st.executeUpdate(str);
		//5、关闭执行块
		st.close();
		//6、关闭连接
		conn.close();
		return result;
	}
}
